import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author dragon
 * @version 1.0
 * @description 数组的复制
 * @date created in 2018-11-13
 */
public class ArrayCopy {

    public static void main(String[] args) {
//		Integer[] array = {1, 2, 4, 6, 32, 345};
//		String[] dest = new String[array.length];
//		//System.arraycopy(array, 0, dest, 0, array.length);
//		System.out.println(Arrays.toString(dest));
//		testClass();
//		//像这样会发生数组存储异常,可以编译,但是运行不通过
//		String[] strings = copyOf(array, array.length, dest.getClass());

        /**
         * 从这一段代码中可以看出,System.arrayCopy() 是浅复制,复制过来的数组里面的元素仅仅只是复制Person对象的地址值
         */
        Person[] arr = new Person[3];
        Person dragon = new Person("dragon", 12);
        Person mars = new Person("mars", 10);
        Person pig = new Person("pig", 20);
        arr[0] = dragon;
        arr[1] = mars;
        arr[2] = pig;
        Person[] newArray = Arrays.copyOf(arr, 10);
        System.out.println("newArray的长度为:" + newArray.length);
        newArray[0].setName("dragon被修改了");
        System.out.println(arr[0].getName());
        //cloneArray();
        int[] array = {1,2,3};

//
//		Integer[] integers = forCopy(array);
    }

    /**
     * 数组的Arrays.copyOf 底层都是调用的System.arrayCopy()方法,所以它也是浅复制 shallow clone
     *
     * @param original
     * @param newLength
     * @param newType
     * @param <T>
     * @param <U>
     * @return
     */
    public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        T[] copy = (Object[].class.equals(newType)) ? (T[]) new Object[newLength] :
                (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }

    /**
     * 测试Class类的地址值是否相等,判断两个对象是否属于同一种类型
     */
    public static void testClass() {
        Object obj = new Person();
        System.out.println(obj.getClass() == Person.class);

        Integer[] array = {1, 2, 5, 764};
        Number[] array2 = {1234};
        System.out.println(array2.getClass() == Number[].class);
        System.out.println(array2.getClass() == Integer[].class);
    }

    /**
     * 数组的clone方法,也是shallow copy,
     */
    public static void cloneArray() {
        Person[] arr = new Person[3];
        Person dragon = new Person("dragon", 12);
        Person mars = new Person("mars", 10);
        Person pig = new Person("pig", 20);
        arr[0] = dragon;
        arr[1] = mars;
        arr[2] = pig;
        Person[] clone = arr.clone();
        Person[] newArray = Arrays.copyOf(arr, 10);
        System.out.println("newArray的长度为:" + newArray.length);
        newArray[0].setName("dragon被修改了");
        System.out.println(arr[0].getName());
    }

    public static <T> T[] forCopy(T[] array) {
        T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length);
        System.arraycopy(array, 0, newArray, 0, array.length);
        return newArray;
    }

}
